package org.androidpn.server.dao.hibernate;

import org.androidpn.server.dao.NotificationDao;
import org.androidpn.server.model.Notification;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by huang on 2017/3/28 0028.
 */
public class NotificationDaoHibernate extends HibernateDaoSupport implements NotificationDao{

    public void saveNotification(Notification notification) {
        getHibernateTemplate().saveOrUpdate(notification);
        getHibernateTemplate().flush();
    }

    public List<Notification> findNotificationsByUsername(String username) {
        List<Notification> lists = getHibernateTemplate().find("from Notification where username=?",username);
        if(lists != null && lists.size()>0){
            return lists;
        }
        return null;
    }

    public void deleteNotification(Notification notification) {
        getHibernateTemplate().delete(notification);
    }

    public void deleteNotificationByUUID(String uuid) {
        List<Notification> notifications = getHibernateTemplate().find("from Notification where uuid=?",uuid);
        if(notifications!=null&&notifications.size()>0){
            Notification notification = notifications.get(0);
            deleteNotification(notification);
        }
    }
}
